package com.rh.bmw4s.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.rh.bmw4s.R;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * http://4s.liexianghudong.com/index.php/home/index/message/id/30
 */

public class FourFragment extends Fragment {
    @BindView(R.id.yuyue_webview)
    WebView webview;
    Unbinder unbinder;
    @BindView(R.id.frag_four_holder)
    View fragFourHolder;

    private RelativeLayout ll_fail = null;
    private int anInt;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_4, container, false);
        unbinder = ButterKnife.bind(this, view);
        anInt = SPUtils.getInstance().getInt(ConfigValue.userId);
        ll_fail = (RelativeLayout) view.findViewById(R.id.ll_load_fail);

        DialogUtils.showProgressDlg(getActivity(), "加载中...");
        WebSettings setting = webview.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    fragFourHolder.setVisibility(View.VISIBLE);
                }
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                DialogUtils.stopProgressDlg();
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                DialogUtils.stopProgressDlg();
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                DialogUtils.stopProgressDlg();
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
                webview.setVisibility(View.GONE);
                ll_fail.setVisibility(View.VISIBLE);
                DialogUtils.stopProgressDlg();
            }

//            @Override
//            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
//                super.onReceivedHttpError(view, request, errorResponse);
//                webview.setVisibility(View.GONE);
//                ll_fail.setVisibility(View.VISIBLE);
//                DialogUtils.stopProgressDlg();
//            }
        });
        webview.loadUrl("http://4s.liexianghudong.com/index.php/home/index/message/id/30/mem_id/" + anInt);
        return view;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            DialogUtils.showProgressDlg(getActivity(), "加载中...");
            webview.clearCache(true);
            webview.loadUrl("http://4s.liexianghudong.com/index.php/home/index/message/id/30/mem_id/" + anInt);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        //回收webview加载的界面
        if (webview != null) {
            webview.destroy();
        }
    }
}
